package com.tianqu.tms.www.statistics.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.www.statistics.dao.TmsMarketCarVendorDayCountDao;
import com.tianqu.tms.www.statistics.dto.CarVendorDto;
import com.tianqu.tms.www.statistics.dto.CarVendorInnerDto;

public class InventoryCarVendorService {

	
	/**
	 * 品牌统计-在库
	 * @return
	 * @throws TmsDaoException
	 */
	public CarVendorDto getCarVendorLibrary(String startDate, String endDate) throws TmsDaoException{

		CarVendorDto carVendorDto = new CarVendorDto();

		// 品牌统计
		TmsMarketCarVendorDayCountDao tmsMarketCarVendorDayCountDao = new TmsMarketCarVendorDayCountDao();
		
		Map<String, CarVendorInnerDto> carVendorMap = tmsMarketCarVendorDayCountDao.getCarsVendor(startDate, endDate);
		
		// 品牌IDlist
		ArrayList<String> vendorIdList = new ArrayList<String>();
		
		// 品牌名称list
		ArrayList<String> vendorNameList = new ArrayList<String>();
		

		for (Map.Entry<String, CarVendorInnerDto> entry : carVendorMap.entrySet()) {
				String tmpVendorId = entry.getValue().getVendorId();
				String tmpVendorName = entry.getValue().getVendorName();
				
				if(!isExist(vendorIdList, tmpVendorId)) {
					vendorIdList.add(tmpVendorId);
				}
			     
				if(!isExist(vendorNameList, tmpVendorName)) {
					vendorNameList.add(tmpVendorName);
				}
		 }
		
		carVendorDto.setVendorIdList(vendorIdList);
		
		carVendorDto.setVendorNameList(vendorNameList);
		
		this.between2Date(startDate, endDate, carVendorMap, vendorIdList, carVendorDto);
		
		return carVendorDto;
	}
	
	/**
	 * 两个日期之间的所有日期列表
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	private void between2Date(String startDate, String endDate, Map<String, CarVendorInnerDto> carVendorMap, ArrayList<String> vendorIdList,CarVendorDto carVendorDto) {
		
		// 日期列表（X轴）
		ArrayList<String> dateList = new ArrayList<String>();
		Map<String, ArrayList<String>> vendorDataMap = new HashMap<String, ArrayList<String>>();
		
		carVendorInfoByDate(startDate, carVendorMap, vendorIdList, dateList,vendorDataMap);
		
		try {
			Date start = DateUtil.string102ToDate(startDate);
			Date end = DateUtil.string102ToDate(endDate);
			

			// 如果start <= end
			while(!DateUtil.compareDate102(start, end)) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(start);
				// 加一天
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				
				start = calendar.getTime();
				
				String tmpDate = DateUtil.DateToString11(start);
				
				carVendorInfoByDate(tmpDate, carVendorMap, vendorIdList, dateList,vendorDataMap);
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		carVendorDto.setDateList(dateList);
		carVendorDto.setVendorDataMap(vendorDataMap);
	}
	
	private void carVendorInfoByDate(String strDate, 
			Map<String, CarVendorInnerDto> carVendorMap,
			ArrayList<String> vendorIdList, 
			ArrayList<String> dateList,
			Map<String, ArrayList<String>> vendorDataMap){
		
		dateList.add(strDate);
		
		
		
		for (int i = 0; i< vendorIdList.size(); i++) {
			String tmpKey = vendorIdList.get(i);
			
			String mapKey = strDate+"-"+tmpKey;
			
			if (vendorDataMap.containsKey(tmpKey)) {
				
				if (carVendorMap.get(mapKey) != null) {
					vendorDataMap.get(tmpKey).add(carVendorMap.get(mapKey).getOnlineCount());
				} else {
					vendorDataMap.get(tmpKey).add("0");
				}
				
			} else {
				ArrayList<String> strValue = new ArrayList<String>();
				
				if (carVendorMap.get(mapKey) != null) {
					strValue.add(carVendorMap.get(mapKey).getOnlineCount());
				} else {
					strValue.add("0");
				}
				
				vendorDataMap.put(tmpKey, strValue);
			}
			
		}
	}
	
	private boolean isExist(ArrayList<String> list, String item) {
		boolean isExist = false;
		for (int i = 0 ; i < list.size(); i++) {
			String tmpItem = list.get(i);
			if (item.equals(tmpItem)) {
				isExist = true;
			}
		}
		
		return isExist;
	}
}
